﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Project_Udrinkwater.CustomerLogin
{
    public partial class RegisterLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = new DataTable(); 
                dt = GetData();

                SqlDataAmphur.SelectCommand = @"SELECT [AMPHUR_ID],[AMPHUR_NAME] FROM [DB_Udrink].[dbo].[TB_Amphur] WHERE AMPHUR_ID =" + dt.Rows[0]["AMPHUR_ID"].ToString() + "";
                SqlDataDistrict.SelectCommand =
                    @"SELECT [DISTRICT_ID],[DISTRICT_NAME] FROM [TB_District] WHERE DISTRICT_ID = " +
                    dt.Rows[0]["DISTRICT_ID"].ToString() + "";

                DropDownList1.SelectedItem.Text = dt.Rows[0]["Prefix"].ToString();
                txtFName.Text = dt.Rows[0]["Firstname"].ToString();
                txtLname.Text = dt.Rows[0]["Lastname"].ToString();
                txtAdress.Text = dt.Rows[0]["Adress"].ToString();
                ddlProvince.SelectedValue = dt.Rows[0]["PROVINCE_ID"].ToString();
                ddlAmphur.SelectedValue = dt.Rows[0]["AMPHUR_ID"].ToString();
                ddlDistrict.SelectedValue = dt.Rows[0]["DISTRICT_ID"].ToString();
                txtPostcode.Text = dt.Rows[0]["Postcode"].ToString();
                txtPhone.Text = dt.Rows[0]["Phone"].ToString();
                txtFax.Text = dt.Rows[0]["Fax"].ToString(); 

               
            }
          
        }

        public void Binddata()
        {
            DataTable dt = new DataTable();
            dt = GetData();

            SqlDataAmphur.SelectCommand = @"SELECT [AMPHUR_ID],[AMPHUR_NAME] FROM [DB_Udrink].[dbo].[TB_Amphur] WHERE AMPHUR_ID =" + dt.Rows[0]["AMPHUR_ID"].ToString() + "";
            SqlDataDistrict.SelectCommand =
                @"SELECT [DISTRICT_ID],[DISTRICT_NAME] FROM [TB_District] WHERE DISTRICT_ID = " +
                dt.Rows[0]["DISTRICT_ID"].ToString() + "";

            DropDownList1.SelectedItem.Text = dt.Rows[0]["Prefix"].ToString();
            txtFName.Text = dt.Rows[0]["Firstname"].ToString();
            txtLname.Text = dt.Rows[0]["Lastname"].ToString();
            txtAdress.Text = dt.Rows[0]["Adress"].ToString();
            ddlProvince.SelectedValue = dt.Rows[0]["PROVINCE_ID"].ToString();
            ddlAmphur.SelectedValue = dt.Rows[0]["AMPHUR_ID"].ToString();
            ddlDistrict.SelectedValue = dt.Rows[0]["DISTRICT_ID"].ToString();
            txtPostcode.Text = dt.Rows[0]["Postcode"].ToString();
            txtPhone.Text = dt.Rows[0]["Phone"].ToString();
            txtFax.Text = dt.Rows[0]["Fax"].ToString(); 
        }
        private static string Connection = ConfigurationManager.ConnectionStrings["DB_UdrinkConnectionString"].ToString();
        private DataTable GetData()
        {
            string sqltext = @"SELECT * From TB_Customer WHERE Customer_ID = '"+Session["User"].ToString()+"' ";
           // string sqltext = @"SELECT * From TB_Customer WHERE Customer_ID = 'Customer69' ";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];

        }
        public void UpdateDataCustomer()
        {
            string sqltext = @"UPDATE TB_Customer SET 
                                 [E_mail] = @Email
                                ,[Prefix] = @Prefix
                                ,[Firstname] = @Fname
                                ,[Lastname] = @Lname
                                ,[Adress] = @Address
                                ,[DISTRICT_ID] = @Subdistrict
                                ,[AMPHUR_ID] = @District
                                ,[PROVINCE_ID] = @Province
                                ,[Postcode] = @Postcode
                                ,[Phone] = @Phone
                                ,[Fax] = @Fax
                               WHERE Customer_ID = '" + Session["User"].ToString()+"'";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@Email", txtE_mail.Text);
            sqlcomm.Parameters.AddWithValue("@Prefix", DropDownList1.SelectedItem.Text);
            sqlcomm.Parameters.AddWithValue("@Fname", txtFName.Text);
            sqlcomm.Parameters.AddWithValue("@Lname", txtLname.Text);
            sqlcomm.Parameters.AddWithValue("@Address", txtAdress.Text);
            sqlcomm.Parameters.AddWithValue("@Subdistrict", ddlDistrict.SelectedValue);
            sqlcomm.Parameters.AddWithValue("@District", ddlAmphur.SelectedValue);
            sqlcomm.Parameters.AddWithValue("@Province", ddlProvince.SelectedValue);
            sqlcomm.Parameters.AddWithValue("@Postcode", txtPostcode.Text);
            sqlcomm.Parameters.AddWithValue("@Phone", txtPhone.Text);
            sqlcomm.Parameters.AddWithValue("@Fax", txtFax.Text);
            
            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose(); 

        }

        protected void bt_Edit_Click(object sender, EventArgs e)
        {
            UpdateDataCustomer();
            Response.Write(@"<script language='javascript'>alert('บันทึกข้อมูลเรียบร้อยแล้ว')</script>");
            Binddata();
                    
        }


        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlAmphur.Items.Clear();
            ddlDistrict.Items.Clear();
        }

        protected void ddlAmphur_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlDistrict.Items.Clear();
        }
    }
}